package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Page;
import com.dongdongshop.data.PageBean;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.GoodsVO;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbGoodsDesc;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private TbGoodsMapper tbGoodsMapper;
    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;
    @Autowired
    private TbItemMapper tbItemMapper;




    @Override
    public boolean inserGoodsDesc(TbGoodsDesc tbGoodsDesc) {
        int i = tbGoodsDescMapper.insertSelective(tbGoodsDesc);
        return i > 0;
    }

//    @Override
//    public boolean inserGoods(String sellerId, TbGoods tbGoods, TbGoodsDesc tbGoodsDesc) {
//        int i =  tbGoodsMapper.insertGoods(sellerId,tbGoods);
//        Long id = tbGoods.getId();
//        int i2 = tbGoodsDescMapper.insertGoodsDesc(id,tbGoodsDesc);
////        tbGoodsDesc.setGoodsId(id);
////        tbGoodsDescMapper.insertSelective(id,tbGoodsDesc);
//        return true;
//    }

    @Override
    public void insertGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, TbItem tbItem) {
        tbGoods.setIsDelete("1");
        tbGoods.setIsEnableSpec("1");
        tbGoods.setAuditStatus("0");
        tbGoods.setIsMarketable("1");
        tbGoodsMapper.insertGoods(tbGoods);
        tbGoodsDesc.setGoodsId(tbGoods.getId());
        tbGoodsDescMapper.insertSelective(tbGoodsDesc);

        tbItem.setSellPoint(tbGoods.getCaption());
        tbItem.setCostPirce(tbGoods.getPrice());
        tbItem.setMarketPrice(tbGoods.getPrice());
        tbItem.setGoodsId(tbGoods.getId());
        tbItem.setTitle(tbGoods.getGoodsName());
        tbItem.setCategoryid(tbGoods.getCategory3Id());
        tbItem.setCreateTime(new Date());
        tbItem.setUpdateTime(new Date());
        tbItem.setStatus("0");

        JSONArray array = JSONObject.parseArray(tbItem.getSpec());
        for (Object o : array) {
            String s = JSONObject.toJSONString(o);
            tbItem.setSpec(s);
            tbItemMapper.insertSelective(tbItem);
        }

    }

    @Override
    public Page<GoodsVO> selectGoods(String sellerId, String auditStatus, String goodsName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);


        List<TbGoods> count = tbGoodsMapper.selectByExample(null);
        PageInfo pageInfo = new PageInfo(count);

        Page page = new Page();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        page.setTatal(pageInfo.getTotal());
        List<GoodsVO> list = tbGoodsMapper.listGoods(sellerId,auditStatus,goodsName,page.getPageNum(),pageSize);
        page.setRows(list);
        return page;
    }

    @Override
    public TbGoods selectByGoodsId(Long goodsId) {
        TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(goodsId);
        return tbGoods;
    }

    @Override
    public PageBean<GoodsVO> listGoods(String id, String auditStatus, String goodsName, Integer pageNum, Integer pageSize) {
        PageBean<GoodsVO> pageBean = new PageBean<>();
        pageBean.setPageNum(pageNum);
        pageBean.setPageSize(pageSize);

        List<TbGoods> count = tbGoodsMapper.selectByExample(null);
        pageBean.setTotal(count.size());


        List<GoodsVO> list = tbGoodsMapper.listGoods(id,auditStatus,goodsName,pageBean.getStartIndex(),pageSize);
        pageBean.setResult(list);
        return pageBean;
    }

    @Override
    public void updateStatusById(Long[] ids, String status) {
        tbGoodsMapper.updateStatusById(ids,status);
    }
}
